package simplecoder.util;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;

import simplecoder.PluginBean;

import com.sun.org.apache.xml.internal.security.Init;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerUtil {
	@SuppressWarnings("rawtypes")
	public static String dataToString(Map data,String templatePath) throws IOException,TemplateException{
		init();
		
		
		Writer out = null;
		Template template = cfg.getTemplate(templatePath);
		out = new StringWriter();
		template.process(data,out);
		return out.toString();
	}
	
	private static Configuration cfg;
    private String templateDir = "templates";
    private String templateName = "DaoInterface.java.ftl";
    private String outputDir = "C:/codegen/output";
	
	public static void init() throws IOException {
        // Initialize the FreeMarker configuration;
        // - Create a configuration instance
		if(cfg==null){
			System.out.println("1");
			System.out.println(PluginBean.getTemplates()+" templates");
			System.out.println("2");
			
	        cfg = new Configuration();
	        File templateDir = new File(PluginBean.getTemplates());
//	        File templateDir = new File("templates");
	        System.out.println("-9900-");
	        cfg.setDirectoryForTemplateLoading(templateDir);
	        cfg.setLocale(Locale.CHINA);
	        cfg.setDefaultEncoding("UTF-8");
	        System.out.println("-----");
		}
    }
}
